package org.fuys.etp.repo;

import org.fuys.etp.core.model.type.EtpConfigChangeTypeEnum;
import org.fuys.etp.core.model.vo.TpConfigHistoryVO;
import org.fuys.etp.core.model.vo.TpConfigVO;
import org.fuys.etp.core.model.vo.TpRegulationStrategyVO;
import org.fuys.etp.repo.po.EtpConfigHistory;
import org.fuys.etp.repo.repository.IEtpConfigRepository;
import org.fuys.etp.repo.repository.IEtpStrategyRepository;
import org.fuys.etp.repo.repository.IEtpUpdateHistoryRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @projectName: fuys-etp
 * @package: org.fuys.etp.repo
 * @className: EtpRepoService
 * @author: WangJing
 * @description: 弹性线程池仓库服务
 * @date: 2025/2/6 23:40
 * @version: 1.0
 */
@Service
public class EtpRepoService {

    @Resource
    private IEtpConfigRepository configRepository;

    @Resource
    private IEtpStrategyRepository strategyRepository;

    @Resource
    private IEtpUpdateHistoryRepository historyRepository;

    public List<TpConfigVO> selectConfigByIds(String poolType, List<Integer> loadConfigIds) {
        return configRepository.selectConfigByIds(poolType,loadConfigIds);
    }

    public TpRegulationStrategyVO selectStrategyById(String strategyId) {
        return strategyRepository.getStrategyById(strategyId);
    }

    public List<TpConfigHistoryVO> selectHistory(String poolType,String poolSize,Integer count){
        return historyRepository.selectHistoryRecord(poolType, poolSize, count);
    }

    @Transactional
    public void persistentUpdateHistory(String poolName,String poolType,Integer oldConfigId,
                                        Integer newConfigId,Long userId){
        TpConfigHistoryVO configHistory = new TpConfigHistoryVO();
        configHistory.setChangeTime(new Date());
        configHistory.setChangeType(userId!=null? EtpConfigChangeTypeEnum.MANUAL.type()
                :EtpConfigChangeTypeEnum.SELF_CHECK.type());
        configHistory.setCurrentConfigVersion(newConfigId);
        configHistory.setChangeUserId(userId);
        configHistory.setPreviousConfigVersion(oldConfigId);
        configHistory.setPoolName(poolName);
        configHistory.setPoolType(poolType);
        historyRepository.insertHistoryRecord(configHistory);
    }

    @Transactional
    public boolean insertConfig(String poolType,TpConfigVO configVO){
        return configRepository.insertConfig(poolType, configVO);
    }

    @Transactional
    public boolean insertStrategy(TpRegulationStrategyVO strategy){
        return strategyRepository.insertStrategy(strategy);
    }

    @Transactional
    public boolean updateConfig(String poolType,Integer configVersion,TpConfigVO config){
        return configRepository.updateConfig(poolType,configVersion,config);
    }

    @Transactional
    public boolean updateStrategy(Integer strategyId,TpRegulationStrategyVO strategy){
        return strategyRepository.updateStrategy(strategyId,strategy);
    }

    @Transactional
    public boolean deleteConfig(String poolType,Integer configVersion,TpConfigVO config){
        return configRepository.deleteConfig(poolType,configVersion);
    }

    @Transactional
    public boolean deleteStrategy(Integer strategyId){
        return strategyRepository.deleteStrategy(strategyId);
    }

    @Transactional
    public boolean deleteHistory(String poolType,String poolName){
        return true;
    }

}
